//author:kakake
class tsMenuDao {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        WorkId: 1
    }

    constructor(workid: number) {
        this.data.WorkId = workid;
    }

    /**获取机构列表 */
    getWorkerData() {
        let strsql = "select * from BaseWorkers";
        return TSLobsterAPI.DbHelper.DbGetJsonTable(this.data.dbConn, strsql);
    }

    getBaseModule() {
        let strsql = "select * from BaseModule order by SortId";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql);
    }

    getCurrModule(moduleId) {
        let strsql = "select * from BaseModule where moduleId=@moduleId";
        return TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { moduleId: moduleId });
    }

    getBaseMenu() {
        let strsql = "select c.MenuId,c.SortId,c.Name,c.DllName SoftName,c.FunName ControllerName,c.FunWcfName ViewName,c.ModuleId,c.PMenuId,c.MenuToolBar,c.MenuLookBar,c.Memo,c.Image,c.UrlId,c.UrlName,c.BindSQL,c.WorkId from BaseMenu c";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql);
    }

    getGroupMenuList(groupId) {
        let strsql = "select distinct c.* from  BaseGroupMenu b  left join BaseMenu c on b.MenuId=c.MenuId where b.GroupId=@groupId and c.MenuId is not null";
        return TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { groupId: groupId });
    }

    deleteGroupMenu(groupId) {
        let strsql = "delete from BaseGroupMenu where GroupId=@groupId";
        return TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { groupId: groupId });
    }

    getMenuPId(menuId) {
        let strsql = "select PMenuId from BaseMenu where MenuId=@menuId";
        return TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, { menuId: menuId });
    }

    getIsGroupMenu(groupId, menuId): boolean {
        let strsql = "select count(1) from BaseGroupMenu where GroupId=@groupId and MenuId=@menuId";
        var ret = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, { groupId: groupId, menuId: menuId });
        return parseInt(ret) > 0;
    }

    saveUserWorkGroup(groupId, menuId) {
        let strsql = "select ModuleId from BaseMenu where MenuId=@menuId";
        var moduleId = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, { menuId: menuId });
        // TSLobsterAPI.Utils.TrackLog("moduleId", moduleId);

        strsql = "INSERT INTO BaseGroupMenu([GroupId], [ModuleId], [MenuId], [WorkId]) VALUES (@groupId, @moduleId, @menuId, @workId)";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { groupId: groupId, moduleId: moduleId, menuId: menuId, workId: this.data.WorkId });
    }

    /**删除角色 */
    deleteGroup(groupId) {
        let strsql = "delete from basegroup where groupid=@groupId";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { groupId: groupId });
    }

    /**删除模块 */
    deleteModule(moduleId) {
        let strsql = "delete from BaseModule where ModuleId=@moduleId";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { moduleId: moduleId });
    }

    /**保存模块 */
    saveModule(model) {
        let strsql = "INSERT INTO BaseModule([Name], [Memo], [SortId], [ServerIP], [WorkId]) VALUES (@Name, '', 1, '', @WorkId)";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { Name: model.Name, WorkId: this.data.WorkId });
    }

    /**删除菜单 */
    deleteMenu(menuId) {
        let strsql = "delete from BaseMenu where menuId=@menuId";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { menuId: menuId });
    }


}